#include <iostream>
#include <fstream>
#include "Graph.h"
#include "Solver.h"
#include "settings.h"
#include "mpi.h"

using namespace std;

inline void usage() {
    cout << "Graph cut with minimal weight" << endl;
    cout << "Usage: par-mhr <filename>" << endl;
}

/*
 * 
 */
int main(int argc, char** argv) {

    int process_rank;
    int processes;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &process_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &processes);

    // no src file appended
    if(argc < 2) {
        usage();
        return 1;
    }

    // nonexistent src file
    fstream inFile;
    inFile.open(argv[1]);
    if (!inFile || inFile.eof()) {
        cerr << "Empty or no file" << endl;
        return 1;
    }

    // read data from src file, assign some weight to all edges
    Graph graph;
    inFile >> graph;

    // no need to bcast graph, parameters are transferred correctly to all processes

    // create solver, start solving
    Solver solver;
    solver.init(&graph, process_rank, processes);
    solver.solve();

    // cout graph and solution
    if (process_rank == 0) {
        cout << graph;
        cout << "Approximate running time: " << solver.getRunningTime() << endl;
        cout << solver.getBestSolution();
    }

    MPI_Finalize();
    return 0;
}

